package org.realogic.cfm.authenticate;

import java.util.List;

import org.realogic.cfm.util.UtilString;

public class KPermissionsImpl implements KPermissions {
	
	private static final long serialVersionUID = -8217101361650461692L;
	
	private List<String> permList;

	public KPermissionsImpl() {

	}

	public KPermissionsImpl(List<String> list_) {
		this.permList = list_;
	}

	public boolean hasPermission(String permType) {
		if (permList == null || permType == null)
			return false;
		if (permType.indexOf("|") != -1) {
			return hasPermission(UtilString.tokenize(permType, "|"), false);
		}
		if (permType.indexOf("@") != -1) {
			return hasPermission(UtilString.tokenize(permType, "@"), true);
		}
		return permList.contains(permType.trim());
	}

	private boolean hasPermission(String[] permType, boolean isAnd) {
		if (permList == null || permType == null) {
			return false;
		}
		boolean flag = false;
		if (isAnd) {
			for (int i = 0; i < permType.length; i++) {
				flag = permList.contains(permType[i].trim());
				if (!flag)
					break;
			}
		} else {
			for (int i = 0; i < permType.length; i++) {
				flag = permList.contains(permType[i].trim());
				if (flag)
					break;
			}
		}
		return flag;
	}

	public boolean isEmpty() {
		if (permList == null)
			return true;
		return permList.isEmpty();
	}
}